﻿using PLCS.Domain.Equipments;
using PLCS.EntityFrameworkCore.EntityFrameworkCore;

namespace PLCS.EntityFrameworkCore.Equipments;

public class EquipmentRepository : EfCoreRepository<PLCSDbContext, Equipment, Guid>, IEquipmentRepository
{
    public EquipmentRepository(IDbContextProvider<PLCSDbContext> dbContextProvider) : base(dbContextProvider)
    {
    }

    public async Task DeleteAllAsync()
    {
        var dbContext = await GetDbContextAsync();
        await dbContext.Database.ExecuteSqlRawAsync("delete from T_Equipments");
    }

    public async Task<Equipment> GetByEquipmentNoAsync(string equipmentNo)
    {
        var queryable = await GetQueryableAsync();
        queryable = queryable.Where(x => x.EquNum == equipmentNo);
        return await AsyncExecuter.FirstOrDefaultAsync(queryable);
    }

    public async Task<IEnumerable<Equipment>> GetByTrackAsync(string trackNo)
    {
        var queryable = await GetQueryableAsync();
        queryable = queryable.Where(x => x.InTrack == trackNo);
        return await AsyncExecuter.ToListAsync(queryable);
    }
}